﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration.AccessControl;
using Microsoft.SharePoint.Administration;

using Northwind.ServiceObjects;

namespace Northwind.ServiceObjects.AdminUI
{
    public class ServiceApplicationAdminPage : GlobalAdminPageBase
    {
        private NorthwindApplication m_NorthwindApplication;

        protected Guid ServiceApplicationID
        {
            get { return (Request.QueryString.Count == 0 || Request.QueryString["ID"] == null) ? Guid.Empty : new Guid(Request.QueryString["ID"].ToString()); }
        }

        protected override bool AccessibleByDelegatedAdminGroup
        {
            get { return HasUserReadPermissionForCurrentPage; }
        }

        protected NorthwindApplication ServiceApplication
        {
            get
            {
                if (null == this.m_NorthwindApplication)
                {
                    NorthwindService service = NorthwindService.Local;

                    if (service == null)
                        throw new InvalidOperationException("Northwind Service is not installed");

                    this.m_NorthwindApplication = service.Applications.GetValue<NorthwindApplication>(ServiceApplicationID);
                }
                return this.m_NorthwindApplication;
            }
        }

        protected bool HasUserReadPermissionForCurrentPage
        {
            get{
                if (ServiceApplication == null)
                    return false;

                if (!SPFarm.Local.CurrentUserIsAdministrator())
                {
                    if ((!ServiceApplication.CheckAdministrationAccess(SPCentralAdministrationRights.Read) && !ServiceApplication.CheckAdministrationAccess(SPCentralAdministrationRights.Write)) && (!ServiceApplication.CheckAdministrationAccess(SPCentralAdministrationRights.None | SPCentralAdministrationRights.ChangePermissions) && !ServiceApplication.CheckAdministrationAccess(~SPCentralAdministrationRights.None)))
                        return false;
                }

                return true;
            }
        }

        public enum PageEditMode
        {
            Create,
            Edit
        }

        protected DialogMaster DialogMaster
        {
            get { return (DialogMaster)Page.Master; }
        }

        protected PageEditMode PageMode
        {
            get
            {
                if (ServiceApplicationID == Guid.Empty)
                {
                    return PageEditMode.Create;
                }
                return PageEditMode.Edit;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            DialogMaster.OkButton.Click += OnOkButtonClick;
        }

        public void OnOkButtonClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                ((DialogMaster)this.Page.Master).OkButton.Visible = false;
                if (this.PageMode == PageEditMode.Create)
                {
                    this.OnCreateApplication();
                }
                else
                {
                    this.OnUpdateApplication();
                }
            }
        }

        protected virtual void OnCreateApplication()
        {
        }

        protected virtual void OnUpdateApplication()
        {
        }
    }
}
